package hunt.treasure.scenes;

import java.util.Random;

import hunt.treasure.GameActivity;
import hunt.treasure.config.ALog;
import hunt.treasure.config.Define;
import hunt.treasure.config.FixedSpriteLocation;
import hunt.treasure.config.FontInfos;
import hunt.treasure.config.ImageInfos;
import hunt.treasure.types.Terrain;
import hunt.treasure.utils.DialogUtils;

import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.modifier.IModifier;

import android.graphics.Color;
import android.graphics.Typeface;
import android.widget.Toast;

/**
 * Main game scene.
 * 
 * @author vanhu_000
 * 
 */
public class GameScene extends AbstractScene {
	private static final String TAG = GameScene.class.getSimpleName();
	private static final Random random = new Random();
	private HUD mHud;

	private ITextureRegion mBackgroundTextureRegion;
	private Sprite mBackgroundSprite;
	private ITextureRegion mPlayerTextureRegion1;
	private ITextureRegion mPlayerTextureRegion2;
	private Sprite mPlayerSprite1;
	private Sprite mPlayerSprite2;
	private ITextureRegion mShopTextureRegion;
	private Sprite mShopSprite;
	private ITextureRegion mItemTextureRegion;
	private Sprite mItemSprite;
	private ITextureRegion mInfoTextureRegion;
	private Sprite mInfoSprite;
	private ITextureRegion mCompassTextureRegion;
	private Sprite mCompassSprite;
	private ITextureRegion mDiceTextureRegion;
	private Sprite mDiceSprite;
	private ITextureRegion mTinyMapTextureRegion;
	private Sprite mTinyMapSprite;
	// clock
	private Font mClockFont;
	private Text mTimerText;
	private int mDiceNumber = 0;
	// maps
	private ITextureRegion mTerrainDesertTextureRegion;
	private ITextureRegion mTerrainForestTextureRegion;
	private ITextureRegion mTerrainPlainTextureRegion;
	private ITextureRegion mTerrainRiverTextureRegion;
	private ITextureRegion mTerrainSwampTextureRegion;
	private ITextureRegion mTerrainHiddenTextureRegion;

	private int mMapSize = 6;
	private Terrain[][] mMap;

	@Override
	public void loadResources() {
		BitmapTextureAtlasTextureRegionFactory
				.setAssetBasePath(Define.BASE_IMAGE_FOLDER);
		final BitmapTextureAtlas bta = new BitmapTextureAtlas(
				activity.getTextureManager(), ImageInfos.BACKGROUND_WIDTH,
				ImageInfos.BACKGROUND_HEIGHT, TextureOptions.DEFAULT);
		mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(bta, activity.getAssets(),
						ImageInfos.BACKGROUND_GAME, 0, 0);
		bta.load();
		// calculate scaled ratio again
		Define.SCALED_RATIO = ((float) GameActivity.CW)
				/ (mBackgroundTextureRegion.getWidth() * Define.SCALED_ORIGINAL_RESOURCE_RATIO);
		// apply for all entities
		float x = mBackgroundTextureRegion.getWidth()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO;
		float y = mBackgroundTextureRegion.getHeight()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO;
		mBackgroundSprite = new Sprite(x, y, mBackgroundTextureRegion, res.vbom);
		mBackgroundSprite.setCullingEnabled(true);
		mBackgroundSprite.setScale(Define.SCALED_RATIO
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO);
		ALog.d(TAG.concat(".loadResources"), "CW=" + GameActivity.CW
				+ ", backgroundWidth=" + mBackgroundTextureRegion.getWidth()
				+ ", scaledRatio=" + Define.SCALED_RATIO);
		// player
		final BitmapTextureAtlas playerTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), ImageInfos.PLAYER_1_WIDTH
						+ ImageInfos.PLAYER_2_WIDTH,
				ImageInfos.PLAYER_1_HEIGHT, TextureOptions.BILINEAR);
		mPlayerTextureRegion1 = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(playerTextureAtlas, activity.getAssets(),
						ImageInfos.PLAYER_1_NAME, 0, 0);
		mPlayerTextureRegion2 = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(playerTextureAtlas, activity.getAssets(),
						ImageInfos.PLAYER_2_NAME, ImageInfos.PLAYER_1_WIDTH, 0);
		playerTextureAtlas.load();
		mPlayerSprite1 = new Sprite(FixedSpriteLocation.PLAYER_SPRITE_1_X,
				FixedSpriteLocation.PLAYER_SPRITE_1_Y, mPlayerTextureRegion1,
				res.vbom);
		mPlayerSprite1.setCullingEnabled(true);
		mPlayerSprite1.setScale(Define.SCALED_RATIO
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO);

		mPlayerSprite2 = new Sprite(mBackgroundTextureRegion.getWidth()
				* Define.SCALED_RATIO * Define.SCALED_ORIGINAL_RESOURCE_RATIO
				- FixedSpriteLocation.PLAYER_SPRITE_1_X,
				FixedSpriteLocation.PLAYER_SPRITE_1_Y, mPlayerTextureRegion2,
				res.vbom);
		mPlayerSprite2.setCullingEnabled(true);
		mPlayerSprite2.setScale(Define.SCALED_RATIO
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO);
		// shop, item, info
		final int shopItemInfoHeight = Math.max(
				Math.max(ImageInfos.INFO_HEIGHT, ImageInfos.ITEM_HEIGHT),
				ImageInfos.SHOP_HEIGHT);
		final int shopItemInfoWidth = ImageInfos.SHOP_WIDTH
				+ ImageInfos.ITEM_WIDTH + ImageInfos.INFO_WIDTH;
		final BitmapTextureAtlas shopItemInfoAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), shopItemInfoWidth,
				shopItemInfoHeight, TextureOptions.BILINEAR);
		mShopTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(shopItemInfoAtlas, activity.getAssets(),
						ImageInfos.SHOP_NAME, 0, 0);
		mItemTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(shopItemInfoAtlas, activity.getAssets(),
						ImageInfos.ITEM_NAME, ImageInfos.SHOP_WIDTH, 0);
		mInfoTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(shopItemInfoAtlas, activity.getAssets(),
						ImageInfos.INFO_NAME, ImageInfos.SHOP_WIDTH
								+ ImageInfos.ITEM_WIDTH, 0);
		shopItemInfoAtlas.load();

		mInfoSprite = new Sprite(mPlayerSprite1.getX(), mPlayerSprite1.getY()
				+ mPlayerSprite1.getHeight()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO + mInfoTextureRegion.getHeight()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO
				+ FixedSpriteLocation.DISTANCE_PLAYER_1_TO_INFO_SPRITE,
				mInfoTextureRegion, res.vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				touchInfoSprite(pSceneTouchEvent, pTouchAreaLocalX,
						pTouchAreaLocalY);
				return true;
			}
		};

		mItemSprite = new Sprite(mPlayerSprite1.getX(), mInfoSprite.getY()
				+ mInfoSprite.getHeight()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO + mItemTextureRegion.getHeight()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO, mItemTextureRegion, res.vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				touchItemSprite(pSceneTouchEvent, pTouchAreaLocalX,
						pTouchAreaLocalY);
				return true;
			}
		};

		mShopSprite = new Sprite(mPlayerSprite1.getX(), mItemSprite.getY()
				+ mItemSprite.getHeight()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO + mShopTextureRegion.getHeight()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO, mShopTextureRegion, res.vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				touchShopSprite(pSceneTouchEvent, pTouchAreaLocalX,
						pTouchAreaLocalY);
				return true;
			}
		};

		mInfoSprite.setCullingEnabled(true);
		mItemSprite.setCullingEnabled(true);
		mShopSprite.setCullingEnabled(true);
		mInfoSprite.setScale(Define.SCALED_RATIO
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO);
		mItemSprite.setScale(Define.SCALED_RATIO
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO);
		mShopSprite.setScale(Define.SCALED_RATIO
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO);
		// compass
		final BitmapTextureAtlas compassTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), ImageInfos.COMPASS_WIDTH,
				ImageInfos.COMPASS_HEIGHT, TextureOptions.BILINEAR);
		mCompassTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(compassTextureAtlas, activity.getAssets(),
						ImageInfos.COMPASS_NAME, 0, 0);
		compassTextureAtlas.load();
		mCompassSprite = new Sprite(mBackgroundTextureRegion.getWidth()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO * Define.SCALED_RATIO
				- FixedSpriteLocation.COMPASS_MARGIN_RIGHT,
				mPlayerSprite2.getY() + mPlayerSprite2.getHeight()
						* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
						* Define.SCALED_RATIO
						+ mCompassTextureRegion.getHeight() / 2
						* Define.SCALED_ORIGINAL_RESOURCE_RATIO
						* Define.SCALED_RATIO, mCompassTextureRegion, res.vbom);
		mCompassSprite.setCullingEnabled(true);
		mCompassSprite.setScale(Define.SCALED_RATIO
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO);
		// dice
		final BitmapTextureAtlas diceTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), ImageInfos.DICE_WIDTH,
				ImageInfos.DICE_HEIGHT, TextureOptions.BILINEAR);
		mDiceTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(diceTextureAtlas, activity.getAssets(),
						ImageInfos.DICE_NAME, 0, 0);
		diceTextureAtlas.load();
		mDiceSprite = new Sprite(mCompassSprite.getX(), mCompassSprite.getY(),
				mDiceTextureRegion, res.vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				touchDiceSprice(pSceneTouchEvent, pTouchAreaLocalX,
						pTouchAreaLocalY);
				return true;
			}
		};
		mDiceSprite.setCullingEnabled(true);
		mDiceSprite.setScale(Define.SCALED_RATIO
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO);
		// tiny map
		final BitmapTextureAtlas minimapTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), ImageInfos.TINY_MAP_WIDTH,
				ImageInfos.TINY_MAP_HEIGHT, TextureOptions.BILINEAR);
		mTinyMapTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(minimapTextureAtlas, activity.getAssets(),
						ImageInfos.TINY_MAP_NAME, 0, 0);
		minimapTextureAtlas.load();
		mTinyMapSprite = new Sprite(mBackgroundSprite.getWidth()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO * Define.SCALED_RATIO
				- FixedSpriteLocation.TINY_MAP_MARGIN_RIGHT
				- mTinyMapTextureRegion.getWidth()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO, mCompassSprite.getY()
				+ mCompassSprite.getHeight()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO + mTinyMapTextureRegion.getHeight()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO / 2
				* Define.SCALED_RATIO, mTinyMapTextureRegion, res.vbom);
		mTinyMapSprite.setCullingEnabled(true);
		mTinyMapSprite.setScale(Define.SCALED_RATIO
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO);
		// clock
		FontFactory.setAssetBasePath(Define.BASE_FONT_FOLDER);
		final Typeface tf = Typeface.createFromAsset(activity.getAssets(),
				Define.BASE_FONT_FOLDER.concat(FontInfos.BOMBING_NAME));
		mClockFont = FontFactory.create(activity.getFontManager(),
				activity.getTextureManager(), 128, 128, tf, 26f, true,
				Color.BLACK);
		mClockFont.load();
		mClockFont.prepareLetters("00:00".toCharArray());
		final float timerTextX = mBackgroundSprite.getWidth()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO * Define.SCALED_RATIO
				- FixedSpriteLocation.CLOCK_TEXT_MARGIN_RIGHT;
		final float timerTextY = mBackgroundSprite.getHeight()
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO * Define.SCALED_RATIO
				- FixedSpriteLocation.CLOCK_TEXT_MARGIN_TOP;
		mTimerText = new Text(timerTextX, timerTextY, mClockFont, "00:00",
				res.vbom) {
			int lastSecond = (int) (System.currentTimeMillis() / 1000);
			int startSecond = lastSecond;

			@Override
			protected void onManagedUpdate(float pSecondsElapsed) {
				final int second = (int) (System.currentTimeMillis() / 1000);
				if (second > lastSecond) {
					final int elapsedTime = second - startSecond;
					int m = elapsedTime / 60;
					int s = elapsedTime % 60;
					String text = "";
					if (m < 10)
						text = "0" + m + ":";
					else
						text = "" + m + ":";
					if (s < 10)
						text = text + "0" + s;
					else
						text = text + s;
					this.setText(text);
					lastSecond = second;
				}

				super.onManagedUpdate(pSecondsElapsed);
			}
		};

		mTimerText.setScale(Define.SCALED_RATIO);
		// terrains
		final BitmapTextureAtlas terrainTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(),
				6 * ImageInfos.TERRAIN_DESERT_WIDTH,
				ImageInfos.TERRAIN_DESERT_HEIGHT, TextureOptions.BILINEAR);
		mTerrainDesertTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(terrainTextureAtlas, activity.getAssets(),
						ImageInfos.TERRAIN_DESERT_NAME, 0, 0);
		mTerrainForestTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(terrainTextureAtlas, activity.getAssets(),
						ImageInfos.TERRAIN_FOREST_NAME,
						ImageInfos.TERRAIN_DESERT_WIDTH, 0);
		mTerrainPlainTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(terrainTextureAtlas, activity.getAssets(),
						ImageInfos.TERRAIN_PLAIN_NAME,
						2 * ImageInfos.TERRAIN_DESERT_WIDTH, 0);
		mTerrainRiverTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(terrainTextureAtlas, activity.getAssets(),
						ImageInfos.TERRAIN_RIVER_NAME,
						3 * ImageInfos.TERRAIN_DESERT_WIDTH, 0);
		mTerrainSwampTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(terrainTextureAtlas, activity.getAssets(),
						ImageInfos.TERRAIN_SWAMP_NAME,
						4 * ImageInfos.TERRAIN_DESERT_WIDTH, 0);
		mTerrainHiddenTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(terrainTextureAtlas, activity.getAssets(),
						ImageInfos.TERRAIN_HIDDEN_NAME,
						5 * ImageInfos.TERRAIN_DESERT_WIDTH, 0);
		terrainTextureAtlas.load();
	}

	@Override
	public void create() {
		setBackground(new SpriteBackground(mBackgroundSprite));
		createHUD();
		createMap();
	}

	@Override
	public void unloadResources() {
		// TODO Auto-generated method stub

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBackKeyPressed() {
		super.onBackKeyPressed();
		activity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				DialogUtils.showConfirmDialog(activity, "Exit",
						"Do you want to exit?",
						new DialogUtils.ConfirmDialogOnClickListener() {

							@Override
							public void onOKButtonOnClick() {
								activity.finish();
							}

							@Override
							public void onCancelButtonOnClick() {

							}
						});
			}
		});
	}

	private void createHUD() {
		mHud = new HUD();
		res.camera.setHUD(mHud);
		mHud.attachChild(mPlayerSprite1);
		mHud.attachChild(mPlayerSprite2);
		mHud.attachChild(mInfoSprite);
		mHud.attachChild(mItemSprite);
		mHud.attachChild(mShopSprite);
		mHud.attachChild(mCompassSprite);
		mHud.attachChild(mDiceSprite);
		mHud.attachChild(mTinyMapSprite);
		mHud.attachChild(mTimerText);
		mHud.registerTouchArea(mInfoSprite);
		mHud.registerTouchArea(mItemSprite);
		mHud.registerTouchArea(mShopSprite);
		mHud.registerTouchArea(mDiceSprite);
	}

	private void createMap() {
		// TODO get from server. This is used only to test
		mMap = new Terrain[mMapSize][mMapSize];
		for (int idx = 0; idx < mMapSize; idx++)
			for (int jdx = 0; jdx < mMapSize; jdx++) {
				int terrainType = random.nextInt(Terrain.TERRAIN_COUNT);
				boolean isHidden = false;
				if (jdx % 3 == 0) {
					isHidden = random.nextBoolean();
				}

				Terrain terrain = new Terrain(terrainType, isHidden);
				mMap[idx][jdx] = terrain;
			}
		// create entity
		Sprite terrain = null;
		float terrainX = 0;
		float terrainY = 0;
		for (int idx = 0; idx < mMapSize; idx++)
			for (int jdx = 0; jdx < mMapSize; jdx++) {

				if (idx == 0) {
					terrainX = idx * mTerrainDesertTextureRegion.getWidth()
							* Define.SCALED_SPRITE_SIZE_RATIO
							+ FixedSpriteLocation.BEGIN_TERRAIN_X;
				} else {
					terrainX = mMap[idx - 1][jdx].getTerrainSprite().getX()
							+ mTerrainDesertTextureRegion.getWidth()
							* Define.SCALED_SPRITE_SIZE_RATIO
							+ FixedSpriteLocation.DISTANCE_BETWEEN_TWO_TERRAIN;
				}

				terrainY = jdx * mTerrainDesertTextureRegion.getHeight()
						* Define.SCALED_SPRITE_SIZE_RATIO
						+ FixedSpriteLocation.BEGIN_TERRAIN_Y;
				final Terrain tr = mMap[idx][jdx];
				if (mMap[idx][jdx].isHidden()) {
					terrain = new Sprite(terrainX, terrainY,
							mTerrainHiddenTextureRegion, res.vbom) {
						@Override
						public boolean onAreaTouched(
								TouchEvent pSceneTouchEvent,
								float pTouchAreaLocalX, float pTouchAreaLocalY) {
							touchTerrainSprite(tr, pSceneTouchEvent,
									pTouchAreaLocalX, pTouchAreaLocalY);
							return true;
						}
					};
				} else {
					switch (mMap[idx][jdx].getTerrainType()) {
					case Terrain.DESERT:
						terrain = new Sprite(terrainX, terrainY,
								mTerrainDesertTextureRegion, res.vbom) {
							@Override
							public boolean onAreaTouched(
									TouchEvent pSceneTouchEvent,
									float pTouchAreaLocalX,
									float pTouchAreaLocalY) {
								touchTerrainSprite(tr, pSceneTouchEvent,
										pTouchAreaLocalX, pTouchAreaLocalY);
								return true;
							}
						};
						break;
					case Terrain.FOREST:
						terrain = new Sprite(terrainX, terrainY,
								mTerrainForestTextureRegion, res.vbom) {
							@Override
							public boolean onAreaTouched(
									TouchEvent pSceneTouchEvent,
									float pTouchAreaLocalX,
									float pTouchAreaLocalY) {
								touchTerrainSprite(tr, pSceneTouchEvent,
										pTouchAreaLocalX, pTouchAreaLocalY);
								return true;
							}
						};
						break;
					case Terrain.PLAIN:
						terrain = new Sprite(terrainX, terrainY,
								mTerrainPlainTextureRegion, res.vbom) {
							@Override
							public boolean onAreaTouched(
									TouchEvent pSceneTouchEvent,
									float pTouchAreaLocalX,
									float pTouchAreaLocalY) {
								touchTerrainSprite(tr, pSceneTouchEvent,
										pTouchAreaLocalX, pTouchAreaLocalY);
								return true;
							}
						};
						break;
					case Terrain.RIVER:
						terrain = new Sprite(terrainX, terrainY,
								mTerrainRiverTextureRegion, res.vbom) {
							@Override
							public boolean onAreaTouched(
									TouchEvent pSceneTouchEvent,
									float pTouchAreaLocalX,
									float pTouchAreaLocalY) {
								touchTerrainSprite(tr, pSceneTouchEvent,
										pTouchAreaLocalX, pTouchAreaLocalY);
								return true;
							}
						};
						break;
					case Terrain.SWAMP:
						terrain = new Sprite(terrainX, terrainY,
								mTerrainSwampTextureRegion, res.vbom) {
							@Override
							public boolean onAreaTouched(
									TouchEvent pSceneTouchEvent,
									float pTouchAreaLocalX,
									float pTouchAreaLocalY) {
								touchTerrainSprite(tr, pSceneTouchEvent,
										pTouchAreaLocalX, pTouchAreaLocalY);
								return true;
							}
						};
						break;
					default:
						break;
					}

				}
				terrain.setScale(Define.SCALED_SPRITE_SIZE_RATIO);
				terrain.setCullingEnabled(true);
				mMap[idx][jdx].setTerrainSprite(terrain);
				registerTouchArea(terrain);
				attachChild(terrain);
			}
	}

	private void touchTerrainSprite(final Terrain terrain,
			TouchEvent pSceneTouchEvent, float pTouchAreaLocalX,
			float pTouchAreaLocalY) {
		if (pSceneTouchEvent.isActionDown())
			activity.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					DialogUtils.showDialog(activity, "Terrain Infos",
							"terrain type=" + terrain.getTerrainType(), null);
				}
			});
	}

	private void touchShopSprite(TouchEvent pSceneTouchEvent,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		// TODO
		if (pSceneTouchEvent.isActionDown())
			activity.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					DialogUtils.showConfirmDialog(activity, "Test",
							"touch at Shop", null);
				}
			});
	}

	private void touchItemSprite(TouchEvent pSceneTouchEvent,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		// TODO
		if (pSceneTouchEvent.isActionDown())
			activity.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					DialogUtils.showConfirmDialog(activity, "Test",
							"touch at Item", null);
				}
			});
	}

	private void touchInfoSprite(TouchEvent pSceneTouchEvent,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		// TODO
		if (pSceneTouchEvent.isActionDown())
			activity.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					DialogUtils.showConfirmDialog(activity, "Test",
							"touch at Info", null);
				}
			});
	}

	private void touchDiceSprice(TouchEvent pSceneTouchEvent,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		// TODO
		if (pSceneTouchEvent.isActionUp()) {
			final RotationModifier modifier = new RotationModifier(
					Define.ROTATED_DICE_DURATION, 0, 1800,
					new IEntityModifier.IEntityModifierListener() {

						@Override
						public void onModifierStarted(
								IModifier<IEntity> pModifier, IEntity pItem) {

						}

						@Override
						public void onModifierFinished(
								IModifier<IEntity> pModifier, IEntity pItem) {
							activity.runOnUiThread(new Runnable() {

								@Override
								public void run() {
									mDiceNumber = random
											.nextInt(Define.MAX_DOT_ON_DICE) + 1;
									Toast.makeText(activity,
											"Number of dot is " + mDiceNumber,
											Toast.LENGTH_SHORT).show();
								}
							});
						}
					});

			mDiceSprite.registerEntityModifier(modifier);
		}
	}
}
